/*
 *  Copyright (C) 2010 Zubanov Dmitry
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jssdb.core.net;

import java.util.ArrayList;

/**
 *
 * @author developer
 */
public class CarrierCollector {

    private ArrayList<CarrierRequestResponse> collectorList = new ArrayList();
    private static final Object sync = new Object();

    private CarrierCollector() {
    }

    public void add(CarrierRequestResponse carrierRequestResponse){
        synchronized (sync) {
            collectorList.add(carrierRequestResponse);
        }
    }

    public boolean isPresent(long id) {
        boolean result = false;
        synchronized (sync) {
            for (CarrierRequestResponse crr : collectorList) {
                if (crr.getId() == id) {
                    result = true;
                    break;
                }
            }
        }
        return result;
    }

    public CarrierRequestResponse getCarrierRequestResponse(long id) {
        CarrierRequestResponse result = null;
        synchronized (sync) {
            for (CarrierRequestResponse crr : collectorList) {
                if (crr.getId() == id) {
                    result = crr;
                    collectorList.remove(crr);
                    break;
                }
            }
        }
        return result;
    }

    public static CarrierCollector getInstance() {
        return CarrierCollectorHolder.INSTANCE;
    }

    private static class CarrierCollectorHolder {

        private static final CarrierCollector INSTANCE = new CarrierCollector();
    }
}
